<template>
  <div class="div_padding">
    <el-form :inline="true" :model="formInline" ref="formInline" class="demo-form-inline" size="small">
      <el-form-item label="编码：" prop="code">
        <el-input v-model="formInline.code" placeholder="关键词"></el-input>
      </el-form-item>
      <el-form-item label="显示值：" prop="label">
        <el-input v-model="formInline.label" placeholder="显示值"></el-input>
      </el-form-item>
      <el-form-item label="状态：" prop="status">
        <el-select v-model="formInline.status" placeholder="状态">
          <el-option label="正常" value=True></el-option>
          <el-option label="停用" value=False></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" v-permission="'Search'" type="primary" @click="filterData">查询</el-button>
        <el-button icon="el-icon-refresh" plain @click="resetForm('formInline')">重置</el-button>
        <el-button icon="el-icon-plus" v-permission="'Create'" type="success" @click="onSubmit">新增</el-button>
        <Insert ref="insert" v-bind:dictData="tableData" @reloadTable="reloadTable"></Insert>
        <Edit ref="edit" v-bind:dictData="tableData" @reloadTable="reloadTable"></Edit>
      </el-form-item>
      <el-button style="float: right" icon="el-icon-refresh-right" circle @click="reloadTable"></el-button>
    </el-form>
    <el-table
      :data="tableData"
      size="mini"
      height="72vh"
      v-loading="loading"
      empty-text="查询结果为空!"
      element-loading-text="数据加载中..."
      element-loading-spinner="el-icon-loading"
      style="width: 100%;"
      row-key="id"
      border>
      <el-table-column
        prop="id"
        label="ID"
        v-if="showId"
        align="center"
        width="120">
      </el-table-column>
      <el-table-column 
        label="序号" 
        type="index" 
        align="center" 
        width="50">
      </el-table-column>
      <el-table-column
        prop="code"
        label="编码"
        align="center"
        width="120">
      </el-table-column>
      <el-table-column
        prop="label"
        label="显示值"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="value"
        label="实际值"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="sort"
        label="排序"
        align="center"
        sortable
        width="120">
      </el-table-column>
      <el-table-column prop="status" width="150" align="center" label="显示状态">
        <template slot-scope="scope">
          <span v-if="scope.row.status == true" style="color:#67C23A;">正常</span>
          <span v-if="scope.row.status == false" style="color:#909399;">停用</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="remark"
        label="备注"
        align="center"
        width="150"
      >
      </el-table-column>
      <el-table-column
        prop="update_datetime"
        label="更新时间"
        align="center"
        width="200">
      </el-table-column>
      <el-table-column
        prop="create_datetime"
        label="创建时间"
        align="center"
        width="200">
      </el-table-column>
      <el-table-column
        label="操作" align="center" width="250" prop="parent" fixed="right">
        <template slot-scope="scope">
            <el-button type="primary" v-permission="'Update'" size="mini" @click="onEdit(scope.$index,scope.row)" icon="el-icon-edit" plain>修改</el-button>
            <el-button type="danger" v-permission="'Delete'" size="mini" icon="el-icon-delete" plain @click="onDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="margin: 5px;">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      background
      :current-page="cur_page"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </div>
  </div>
</template>
<script>
  import { getDictData,delDictData } from '@/api/dict'
  import { successMsg } from '@/utils/tools'
  import Insert from '../dicts/insert'
  import Edit from '../dicts/edit'
  export default {
    name:'Dicts',
    components:{
      Insert,
      Edit
    },
    data() {
      return {
        showId:false,
        loading:true,
        tableData: [],
        total:0,
        cur_page:1,
        pageSize:10,
        formInline: {
          code:'',
          label: '',
          status: ''
        }
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      fetchData() {
        getDictData().then(res=>{
          this.tableData=this.$utils.toArrayTree(res.data.data,{ parentKey: 'parent', strict: false })
          this.total = res.data.total
          this.loading=false
        })
      },
      onSubmit(){
        this.$refs.insert.dialogFormVisible = true
      },
      onEdit(index,row){
        this.$refs.edit.dialogFormVisible = true
        this.$refs.edit.formInline = Object.assign({}, row);//formInline，将row的数据复制到editForm对象中，实现的是浅拷贝
      },
      onDelete(row){
        this.$confirm('此操作将真实删除该机构,谨慎操作!是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(()=>{
          delDictData(row.id).then(res=>{
            if(res.code===2000) {
              successMsg(res.msg)
              this.reloadTable()
            }
          }).catch(()=>{
            errorMsg('删除失败')
          })
        }).catch(()=>{
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        })
      },
      reloadTable(){
        this.loading=true
        this.fetchData()
      },
      filterData(){
        let params = {}
        if(this.formInline.code){
          params.code=this.formInline.code
        }
        if(this.formInline.label){
          params.label=this.formInline.label
        }
        if(this.formInline.status){
          params.status=this.formInline.status
        }
         params.page=this.cur_page
        params.limit=this.pageSize
        this.loading=true
        getDictData(params).then(res=>{
          this.tableData=this.$utils.toArrayTree(res.data.data,{ parentKey: 'parent', strict: false })
          this.loading=false
        })
      },
       handleSizeChange(val) {
        this.pageSize=val
        this.loading=true
        this.filterData()
      },
      handleCurrentChange(val) {
        this.cur_page=val
        this.loading=true
        this.filterData()
      },
      resetForm(formName){
        this.$refs[formName].resetFields()
        this.reloadTable()
      }
    },
  }
</script>
<style lang="scss" scoped>
  .div_padding {
    padding: 10px 10px
  }
</style>
